import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from helper.common import logger

class Email_Sender:
    def __init__(self,
                 smtp_server: str,
                 smtp_port: int,
                 sender_email: str,
                 password: str):
        self.smtp_server = smtp_server
        self.smtp_port = smtp_port
        self.sender_email = sender_email
        self.password = password

    def read_file(self, file_path: str) -> str:
        with open(file_path, 'r', encoding='utf-8') as file:
            return file.read()

    def send_email(self,
                   subject: str,
                   body: str,
                   recipients: list):
        try:
            for recipient in recipients:
                server = smtplib.SMTP_SSL(self.smtp_server, self.smtp_port)
                server.login(self.sender_email, self.password)
                msg = MIMEMultipart()
                msg['From'] = self.sender_email
                msg['To'] = recipient
                msg['Subject'] = subject
                msg.attach(MIMEText(body, 'html'))

                server.send_message(msg)
                logger.info(f"Send email success, receiver(s): {recipient}")
        except Exception as e:
            logger.error(f"Send email failed: {e}")
